namespace Core.Base
{
    using System;
    using Contracts.Interfaces;
    using System.Collections.Generic;
    using System.ComponentModel;

    /// <summary>
    /// Agent base class
    /// </summary>
    public abstract class AgentBase : PropertyChangedBase, IAgent, IEquatable<AgentBase>, IEquatable<IAgent>
    {
        private string _name;
        private readonly IEnumerable<IExtension> _extensions;
        private Uri _portrait;

        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.PropertyChangedBase"/> class.
        /// </summary>
        private AgentBase(){ }

        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.PropertyChangedBase"/> class.
        /// </summary>
        protected internal AgentBase(string name, IEnumerable<IExtension> extensions)
        {
            if(string.IsNullOrEmpty(name))throw new ArgumentNullException("name");
            if(extensions ==null)throw new ArgumentNullException("extensions");
            _name = name;
            _extensions = extensions;

            foreach (var extension in _extensions)
                ((INotifyPropertyChangedEx)extension).PropertyChanged += OnExtensionPropertyChanged;
        }


        /// <summary>
        /// Obtains or defines the agent name
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("Name");
                _name = value;
                NotifyPropertyChange(()=>Name);
            }
        }

        /// <summary>
        /// Obtains the agent extensions
        /// </summary>
        public IEnumerable<IExtension> Extensions
        {
            get { return _extensions; }
        }
       
        /// <summary>
        /// Obtains or defines the image uri
        /// </summary>
        public Uri ImageSource
        {
            get { return _portrait; }
            set { _portrait = value; NotifyPropertyChange(()=>ImageSource, ()=>HasImageSource); }
        }

        /// <summary>
        /// Determines if the ImageSource Uri is set
        /// </summary>
        public bool HasImageSource
        {
            get { return _portrait != null; }
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IAgent other)
        {
            if (ReferenceEquals(null, other)) return false;
            return ReferenceEquals(this, other) || Equals(other.Name, _name);
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return _name;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(AgentBase other)
        {
            if (ReferenceEquals(null, other)) return false;
            return ReferenceEquals(this, other) || String.Equals(other._name, _name,StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof (AgentBase) && Equals((AgentBase) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return base.GetHashCode() ;
        }
        /// <summary>
        /// Agent extension PropertyChanged event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnExtensionPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NotifyPropertyChange(e.PropertyName);
        }
    }
}